import upFile
import os
import configparser
import sys
import time
import datetime


def up_file_to_servers():
    cf = configparser.ConfigParser()
    cf.read("./conf/deal.conf", encoding='UTF-8')

    server_ip = cf.get("ftp_up", "up_server")
    server_user = cf.get("ftp_up", "up_server_user")
    server_pwd = cf.get("ftp_up", "up_server_pwd")
    accPath = cf.get("ftp_up", "acc_path")

    my_ftp = upFile.UpFTP(server_ip)
    my_ftp.login(server_user, server_pwd)

    target = "source/acc/"

    for root, ds, fs in os.walk(accPath):
        if root == accPath:
            for f in fs:
                dayStr = f[-12:-4]
                if f.find(day) > 0:
                    sourcePath = root + f
                    try:
                        my_ftp.upload_file(sourcePath, target + f)
                    except Exception as err:
                        print(str(sourcePath) + str(err))

                d1 = datetime.datetime.strptime(dayStr, '%Y%m%d')
                d2 = datetime.datetime.today()
                cha = d2 - d1
                if cha.days > 7:
                    os.remove(root + f)

    my_ftp.close()


day = (datetime.datetime.today() + datetime.timedelta(days=-1)).strftime("%Y%m%d")
if len(sys.argv) > 1:
    day = str(sys.argv[1])
up_file_to_servers()
